﻿using System.Linq;

using SPR.Helpers;

using Styx;

using TreeSharp;

namespace SPR.Classes.Shaman
{
    using global::SPR.Lists;

    class Elemental : RotationBase
    {
        public override string Name
        {
            get { return "Elemental Shaman"; }
        }

        public override string KeySpell
        {
            get { return "Thunderstorm"; }
        }

        // adding some help about cooldown management
        public override string Help
        {
            get
            {
                return  "\n" +
                        "----------------------------------------------------------------------\n" +
                        "Fire DPS totem down (Searing, Fire Elemental)\n"+
                        "Flame Shock DoT on the target\n" +
                        "Lava Burst (whenever it's up)\n"+
                        "Earth Shock (7-9 charges of Lightning Shield)\n"+
                        "Lightning Bolt filler\n"+
                        "----------------------------------------------------------------------\n";
            }
        }

        public override Composite SingleRotation
        {
            get
            {
                return new Sequence(
                    new PrioritySelector(
                        new Decorator(
                            ret => Units.IsTargetWorthy(Me.CurrentTarget),
                                new PrioritySelector(
                                        Item.UseTrinkets(),
                                        Spells.UseRacials(),
                                        Buff.CastBuff("Lifeblood", ret => true, "Lifeblood"), // Thanks Kink
                                        Items.UseBagItem("Volcanic Potion", ret => Buffs.UnitHasHasteBuff(Me), "Volcanic Potion Heroism/Bloodlust"),
                                        Items.UseEngineerGloves(ret => true, "Engineer's gloves"))),
                    // Extra button automation
                        Items.RunMacroText("/click ExtraActionButton1", ret => (Units.IsFadingLight() || Units.IsShrapnel() || Units.IsHourofTwilight()) && SPR.Instance.AutoExtraActionButton, "[Push Button] ExtraActionButton1"),
                        Buff.CastBuff("Lightning Shield", ret => !Buff.PlayerHasBuff("Lightning Shield"), "No Shield Buff - Lightning Shield"),
                        Items.RunMacroText("/cast Call of the Elements", ret => !(Helpers.Player.ChkTotem("Searing Totem") || Helpers.Player.ChkTotem("Fire Elemental Totem")), "Calll of the Elements"),
                    // Interupts & Steal Buffs                    
                      //Spells.CastInterupt("Wind Shear", ret => true, "Wind Shear"),
                    // Cooldowns
                        Spells.CastSpell("Thunderstorm", ret => Me.ManaPercent < 10, "Thunderstorm For Mana"),
                        Spells.CastSelfSpell("Elemental Mastery", ret => Units.IsTargetWorthy(Me.CurrentTarget), "Elemental Mastery"),
                    // AoE
                        new Decorator(
                                ret => !Me.IsMoving && Units.CountEnnemiesInRange(Me.CurrentTarget.Location, 15) > 2 && SPR.Instance.AutoAoe,
                                    new PrioritySelector(
                                        Items.RunMacroText("/startattack", ret => !Me.IsAutoAttacking, "Start auto attack"),
                                        Spells.CastSpell("Chain Lightning", ret => true, "AOE - Chain Lightning")
                                        )),
                    // Default Rotaion
                        Buff.CastDebuff("Flame Shock", ret => !Buff.TargetHasDebuff("Flame Shock"), "Flame Shock"),
                        Spells.CastSpell("Lava Burst", ret => true, "Lava Burst"),
                        Spells.CastSpell("Earth Shock", ret => Buff.PlayerCountBuff("Lightning Shield") >= 7 && Buff.TargetDebuffTimeLeft("Flame Shock").TotalSeconds > 2, "Earth Shock"),
                        Spells.CastSpell("Lightning Bolt", ret => true, "Lightning Bolt"),
                        Spells.CastSpell("Lightning Bolt", ret => Me.IsMoving, "Lightning Bolt")
                        ));
            }
        }

        public override Composite Medic
        {
            get
            {
                return new Decorator(
                    ret => Me.HealthPercent < 100,
                    new PrioritySelector(
                        Items.UseBagItem("Healthstone", ret => Me.HealthPercent < 30, "Healthstone")));
            }
        }

        public override Composite PreCombat
        {
            get
            {
                return new Decorator(
                            ret => !Me.Mounted && !Me.Dead && !Me.Combat && !Me.IsFlying && !Me.IsOnTransport && !Me.HasAura("Food") && !Me.HasAura("Drink"),
                            new PrioritySelector(
                                Buff.CastBuff("Lightning Shield", ret => !Buff.PlayerHasBuff("Lightning Shield"), "No Shield Buff - Lightning Shield"),
                                Buff.CastBuff("Flametongue Weapon", ret => Me.Inventory.Equipped.MainHand.TemporaryEnchantment.Id != 5, "Flametongue Weapon")));
            }
        }

        public override Composite PVPRotation
        {
            get { return SingleRotation; }
        }

        public override Composite PVERotation
        {
            get { return SingleRotation; }
        }
    }
}
